import 'package:aone_common/common.dart';
import 'package:aone_common/entities/entities.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:prototype/theme/custom_theme.dart';
import 'package:prototype/views/game_category/widgets/favorite_icon_btn.dart';
import 'package:prototype/views/game_category/widgets/rating_bar.dart';
import 'package:prototype/views/game_category/widgets/start_game_button.dart';

class FavoriteDrawerController extends GetxController {
  static FavoriteDrawerController get to => Get.find();
  final GlobalKey<AnimatedListState> listKey = GlobalKey<AnimatedListState>();
}

class FavoriteDrawer extends StatelessWidget {
  const FavoriteDrawer({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Get.put(FavoriteDrawerController());
    final ctrl = FavoriteDrawerController.to;
    final state = FavoriteLogic.to.state;

    return Drawer(
      child: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              'my.favorite'.tr,
              style: TextStyle(
                fontSize: 16.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
            Expanded(
              child: Obx(
                () => AnimatedList(
                  initialItemCount: state.favoriteList.length,
                  key: ctrl.listKey,
                  padding: EdgeInsets.symmetric(
                    vertical: 10.h,
                    horizontal: 10.w,
                  ),
                  itemBuilder: (context, index, animation) {
                    return Obx(
                      () => _favoriteItem(
                          state.favoriteList[index], animation, index),
                    );
                  },
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _favoriteItem(
      FavoriteItems item, Animation<double> animation, int index) {
    final logic = FavoriteLogic.to;
    final state = FavoriteLogic.to.state;

    return Container(
      height: 75.w,
      margin: EdgeInsets.only(bottom: 10.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        color: CustomTheme.colorSchema.secondary,
        boxShadow: [
          CustomTheme.boxShadowSm,
        ],
      ),
      clipBehavior: Clip.antiAlias,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          CachedNetworkImage(
            fit: BoxFit.fill,
            width: 75.w,
            height: 75.w,
            imageUrl: item.imgCn!,
          ),
          SizedBox(
            width: 7.w,
          ),
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  item.nameCn!,
                  textAlign: TextAlign.start,
                  style: TextStyle(
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w500,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                Rating(heat: item.heat!),
                DecoratedBox(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(4),
                    color: const Color.fromRGBO(112, 128, 145, 0.6),
                  ),
                  child: Padding(
                    padding: EdgeInsets.symmetric(
                      vertical: 3.0.h,
                      horizontal: 5.w,
                    ),
                    child: Text(
                      'game.type.${item.type}'.tr,
                      style: TextStyle(
                        fontSize: 8.sp,
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 8.w),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                GestureDetector(
                  child: SizedBox(
                    height: 28.h,
                    child: favoriteIcon(1),
                  ),
                  onTap: () {
                    onRemoveItem(item, index);
                  },
                ),
                StartGameButton(params: item),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Future<void> onRemoveItem(FavoriteItems items, int index) async {
    final ctrl = FavoriteDrawerController.to;
    final logic = FavoriteLogic.to;
    final categoryLogic = GameCategoryLogic.to;

    await logic.removeFavorite(gameSubId: items.gameSubId!);
    ctrl.listKey.currentState?.removeItem(
      index,
      (context, animation) => FadeTransition(
        opacity: animation.drive(
          Tween(
            begin: 0,
            end: 1,
          ),
        ),
        child: _favoriteItem(items, animation, index),
      ),
    );
    categoryLogic.changeFavoriteState(
      items.gameSubId!,
      items.subTag!,
      0,
    );
  }
}
